//logs.js
// const util = require('../../utils/util.js')
// var WXBizDataCrypt = require('../../utils/WXBizDataCrypt.js');
const app = getApp()
var AppId = "wx978a492eda6db690";
var AppSecret = '5ae82d1058f1d0e59ef0ad8c344c4f95';

Component({
  options: {
    addGlobalClass: true,
    Message:0
  },
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    haveImg: app.globalData.haveImg,
    bgimg1: app.globalData.bgimg,
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () { 
      //一开始得到了数据 但是不是一加载就显示 现在在组件初始时加载 修复了bug
      this.setData({
        islogin: app.globalData.islogin,
        src: app.globalData.userInfo.userInfo.avatar,
        nickName: app.globalData.userInfo.userInfo.name,
        // Message:app.globalData.MessageCount.length,
        Friends: app.globalData.FriendCount.length,
        Rooms: app.globalData.RoomCount.length,
        bgimg1: app.globalData.bgimg,
        haveImg: app.globalData.haveImg
      })
      var that=this
      // console.log(app.globalData.userInfo.token)
      //每次都要更新看是否有新的消息
      console.log("OK")
      wx.request({
        url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/getHistorySystemInform',
        method: 'POST',
        data: {
          token: app.globalData.userInfo.token
        },
        success: function (e) {
          console.log(e)
          that.setData({
            Message:e.data.list.length
          })
          // that.setData({
          //   list: e.data.list
          // })
        }
      })

    },
    moved: function () { 
      this.refresh()
    },
    detached: function () { },
  },
  methods: {
  getMyInfo: function (e) {
    var that =this;
    app.onLaunch()
    // app.globalData.userInfo = e.detail.userInfo
    // console.log(e.detail.userInfo)
    wx.setStorage({
      key: "loginInformation",
      data: e.detail.userInfo
    })
    that.setData({
      src: e.detail.userInfo.avatarUrl,
      nickName: e.detail.userInfo.nickName,
      islogin:true
    })
    app.globalData.islogin=true
    // console.log(e.detail.userInfo)
    // app.globalData.userInfo=e.detail.userInfo.user
    //获得用户信息
    wx.login({
      success: function (r) {
        var code = r.code;//登录凭证
        // console.log(code)
        if (code) {
          //2、调用获取用户信息接口
          wx.getUserInfo({
            success:function(res)
            {
              app.globalData.login=res
              app.globalData.code=code
              console.log(res)
              wx.request({
                url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/loginInfo/login',
                method: 'POST',
                data: {
                  code:code,
                  rawData: res.rawData,
                  signature: res.signature,
                  encrypteData: res.encryptedData,
                  iv: res.iv,
                  APPID: "wx978a492eda6db690",
                  APPSecret:"5ae82d1058f1d0e59ef0ad8c344c4f95"
                },
                success: function (res) {
                  console.log(res)
                  app.globalData.userInfo = res.data
                  // this.refresh()
                  //得到roomlist从而得到用户数目
                  wx.request({
                    // url: 'https://mockapi.eolinker.com/SN5C9Ybd511179abd037ba7a2443cbd1f4885103f842b3e/chat/getChatRoomList',
                    url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/getChatRoomListByUserId',
                    method: 'POST',
                    data: {
                      token: app.globalData.userInfo.token,
                      userId: app.globalData.userInfo.userInfo.id
                    },
                    success: function (res) {
                      console.log(res)
                      app.globalData.RoomCount=res.data.list
                      that.setData({
                        Rooms: res.data.list.length,
                        // Massage:0
                      })
                    }
                  })
                  //得到userlist 从而得到number
                  wx.request({
                    // url: 'https://mockapi.eolinker.com/SN5C9Ybd511179abd037ba7a2443cbd1f4885103f842b3e/user/getFriendsList',
                    url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/getFriendsList',
                    method: 'POST',
                    data: {
                      token: app.globalData.userInfo.token,
                      userId: app.globalData.userInfo.userInfo.id
                    },
                    success: function (res) {
                      console.log(res)
                      //除非有改动否则不用重新刷新
                      app.globalData.FriendCount=res.data.list
                      that.setData({
                        Friends: res.data.list.length
                      })

                    }
                  })
                  //得到message
                  wx.request({
                    url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/getHistorySystemInform',
                    method: 'POST',
                    data: {
                      token: app.globalData.userInfo.token
                    },
                    success: function (e) {
                      console.log(e)
                      that.setData({
                        Message: e.data.list.length
                      })
                      // that.setData({
                      //   list: e.data.list
                      // })
                    }
                  })
                  

                }
              })   

            }
            
          })
               
        }
      }
    })
    

    },
  refresh(){
    //得到roomlist从而得到用户数目
    wx.request({
      // url: 'https://mockapi.eolinker.com/SN5C9Ybd511179abd037ba7a2443cbd1f4885103f842b3e/chat/getChatRoomList',
      url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/getChatRoomListByUserId',
      method: 'POST',
      data: {
        token: app.globalData.userInfo.token,
        userId: app.globalData.userInfo.userInfo.id
      },
      success: function (res) {
        console.log(res)
        app.globalData.RoomCount = res.data.list
        that.setData({
          Rooms: res.data.list.length,
          // Massage:0
        })
      }
    })
    //得到userlist 从而得到number
    wx.request({
      // url: 'https://mockapi.eolinker.com/SN5C9Ybd511179abd037ba7a2443cbd1f4885103f842b3e/user/getFriendsList',
      url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/getFriendsList',
      method: 'POST',
      data: {
        token: app.globalData.userInfo.token,
        userId: app.globalData.userInfo.userInfo.id
      },
      success: function (res) {
        console.log(res)
        //除非有改动否则不用重新刷新
        app.globalData.FriendCount = res.data.list
        that.setData({
          Friends: res.data.list.length
        })

      }
    })
    //得到message
    wx.request({
      url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/getHistorySystemInform',
      method: 'POST',
      data: {
        token: app.globalData.userInfo.token
      },
      success: function (e) {
        console.log(e)
        that.setData({
          Message: e.data.list.length
        })
        // that.setData({
        //   list: e.data.list
        // })
      }
    })


  },

  goToMember(e) 
  {
    console.log(e)
    var type=e.currentTarget.dataset.type
    wx.navigateTo({
      url: '/pages/home/member/member?type='+type,
    })
  },
  goTosave(e) {
    // wx.navigateTo({
    //   url: '/pages/save/save',
    // })
  },
  goTomes(e) {
    // wx.navigateTo({
    //   url: '/pages/mes/mes',
    // })
  },
  goToset(e) {
    wx.navigateTo({
      url: '/pages/others/globalSetting/globalSetting',
    })
  },
  goToself(e) {
    wx.navigateTo({
      url: '/pages/others/self/self'
    })
  },
  goTohistory(e) {
    wx.navigateTo({
      url: '/pages/others/history/history',
    })
  },
  goToreact(e) {
    // wx.navigateTo({
    //   url: '/pages/other/react/react',
    // })
  }
  }

})
